package com.wxy.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.entity.Staff;
import com.wxy.entity.StaffDetailWages;
import com.wxy.entity.Staffinfor;
import com.wxy.mapper.StaffDetailWagesMapper;
import com.wxy.mapper.StaffMapper;
import com.wxy.mapper.StaffinforMapper;
import com.wxy.vo.SelectNameId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxy
 * @since 2022-01-12
 */
@RestController
@RequestMapping("/staffInfor")
public class StraffinforController {

    @Autowired
    private StaffDetailWagesMapper staffDetailWagesMapper;
    @Autowired
    private StaffinforMapper staffinforMapper;
    @Autowired
    private StaffMapper staffMapper;

    @GetMapping("/getStaffDetailData/{page}")
    public IPage getStaffDetailData(
            @PathVariable("page") Integer pageNum,
            HttpServletRequest request
    ){
        pageNum = (pageNum-1)*10;
        Integer userId =  Integer.valueOf(request.getHeader("userId").toString());
        IPage page = new Page();
        page.setRecords(staffDetailWagesMapper.selectAllDetail(userId,pageNum));
        page.setTotal(staffDetailWagesMapper.selectDetailWagesTotil(userId));
        return page;
    }

    //薪资详细数据查询by时间
    @GetMapping("/getStaffDetailDataByTime/{time}")
    public IPage getStaffDetailDatabyTime(
            @PathVariable("time") Date time,
            HttpServletRequest request
            ){
        IPage page = new Page();
        page.setRecords(staffDetailWagesMapper.selectAllDetailByTime(Integer.valueOf(request.getHeader("userId").toString()),time));
        return page;
    }
    //薪资详细数据查询byNameId
    @GetMapping("/selectStaffDataBystaffId/{staffId}")
    public IPage selectStaffDataBystaffId(
            @PathVariable("staffId") Integer staffId,
            HttpServletRequest request
    ){
        IPage page = new Page();
        page.setRecords(staffDetailWagesMapper.selectAllDetailByStaffId(Integer.valueOf(request.getHeader("userId").toString()),staffId));
        return page;
    }


    //查询所以NameID
    @GetMapping("/getStaffDetailDataByNameId/")
    public List<SelectNameId> getStaffDetailDataByNameId(
            HttpServletRequest request
    ){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",Integer.valueOf(request.getHeader("userId").toString()));
        queryWrapper.select("name","staffId");
        List<Staff> NameIdList =  staffMapper.selectList(queryWrapper);
        List<SelectNameId> selectNameIdList = new ArrayList<>();
        for (Staff staff : NameIdList) {
            SelectNameId selectNameId = new SelectNameId();
            selectNameId.setLabel(staff.getName()+staff.getStaffId());
            selectNameId.setValue(staff.getStaffId());
            selectNameIdList.add(selectNameId);
        }
        return selectNameIdList;
    }

    //更新数据updataStaffWages
    @PostMapping("/updataStaffWages")
    public Boolean updataStaffWages(
            @RequestBody Staffinfor staffinfor,
            HttpServletRequest request
    ){
        //设置时间
        Date date=new Date(System.currentTimeMillis());
        staffinfor.setTime(date);
        Integer userId =  Integer.valueOf(request.getHeader("userId").toString());
        staffinfor.setUserId(userId);
        staffinforMapper.insert(staffinfor);
        //修改staff表中他的wages
        Integer addWages = Integer.valueOf(staffinfor.getCommission());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("staffId",staffinfor.getStaffId());
        Staff selectOne =  staffMapper.selectOne(queryWrapper);
        Integer newWages = selectOne.getWages()+staffinfor.getCommission();
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("userId",userId);
        updateWrapper.eq("staffId",staffinfor.getStaffId());
        selectOne.setWages(newWages);
        staffMapper.update(selectOne,updateWrapper);
        return true;
    }

    //认识变动添加
    @PostMapping("/addStaff")
    public Boolean addStaff(
            @RequestBody Staff staff,
            HttpServletRequest request
    ){
        Integer userId =  Integer.valueOf(request.getHeader("userId").toString());
        //1.先检查工号是否已经存在
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("staffId",staff.getStaffId());
        if (staffMapper.selectOne(queryWrapper)!=null){
            return false;
        }
        staff.setUserId(userId);
        staff.setWages(0);
        staffMapper.insert(staff);
        return true;
    }
    //人员删除
    @PostMapping("/deleStaff")
    public Boolean deleStaff(
            HttpServletRequest request,
            @RequestBody JSONObject jsonObject
    ){
        Integer staffId = Integer.valueOf(jsonObject.getString("staffId"));
        Integer userId = Integer.valueOf(request.getHeader("userId").toString());
        //1.删除Staff表
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("staffId",staffId);
        staffMapper.delete(queryWrapper);
        //2.删除StaffInfor表的
        staffinforMapper.delete(queryWrapper);
        return true;
    }
}

